package com.ciwong.epaper.modules.reciteWords.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.media.PlaybackParams;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.evaluate.bean.WordDetail;
import com.ciwong.epaper.modules.reciteWords.util.WordJumpManager;
import com.ciwong.libs.audio.play.AudioPlayer;
import com.ciwong.libs.audio.play.OnPlayListener;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.utils.ToastUtil;

import java.util.ArrayList;

import static com.ciwong.epaper.modules.reciteWords.ui.ReciteWordDetailActivity.INTENT_FROM_WORD_STUDY_ACTIVITY;

public class VoiceToEnglishView extends StudyWordMoudleView implements View.OnClickListener {

    private Context mContex;
    private WordDetail currentWord;
    private ArrayList<WordDetail> wordDetails=new ArrayList<WordDetail>();

    private ImageView item_1_result,item_2_result,item_3_result,item_4_result,word_voice;
    private TextView item_1,item_2,item_3,item_4;
    private NextStepLinstener nextStepLinstener;
    private boolean isLock=true;

    public VoiceToEnglishView(Context context) {
        super(context);
        mContex=context;

        LayoutInflater.from(this.getContext()).inflate(R.layout.audio_to_english, this);
        initView();
    }

    public VoiceToEnglishView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContex=context;

        LayoutInflater.from(this.getContext()).inflate(R.layout.audio_to_english, this);
        initView();
    }

    public VoiceToEnglishView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContex=context;

        LayoutInflater.from(this.getContext()).inflate(R.layout.audio_to_english, this);
        initView();
    }

    @Override
    public void updataui() {
        try{
            isLock=true;

            try {
                if(!NetworkUtils.isOnline()){
                    ToastUtil.INSTANCE.toastCenterNoNetError();
                }else {
                    AudioPlayer.getInstance().play(currentWord.getWordFile());
                    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M && EApplication.getInstance().mPlayerspeed !=1.0f) {
                        MediaPlayer mMediaPlayer = AudioPlayer.getInstance().getMediaPlayer();
                        if (mMediaPlayer != null) {
                            try {
                                PlaybackParams params = mMediaPlayer.getPlaybackParams().setSpeed(EApplication.getInstance().mPlayerspeed);
                                if (params != null) {
                                    mMediaPlayer.setPlaybackParams(params);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }

                }
            }catch (Exception e){
                e.getStackTrace();
            }



            item_1_result.setVisibility(GONE);
            item_2_result.setVisibility(GONE);
            item_3_result.setVisibility(GONE);
            item_4_result.setVisibility(GONE);

            item_1.setBackground(getResources().getDrawable(R.drawable.item_default_shape));
            item_2.setBackground(getResources().getDrawable(R.drawable.item_default_shape));
            item_3.setBackground(getResources().getDrawable(R.drawable.item_default_shape));
            item_4.setBackground(getResources().getDrawable(R.drawable.item_default_shape));
        }catch (Exception e){
            e.getStackTrace();
            nextStepLinstener.nextStep("VOICE_TO_CHINESE");
        }


    }
    OnPlayListener playListener=new OnPlayListener() {
        @Override
        public void onReadPlayer(long l, Object o) {

        }

        @Override
        public void onPlayStart(Object o) {
            Drawable drawable = word_voice.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable) {
                ((AnimationDrawable) drawable).start();
            }
        }

        @Override
        public void stop(Object o) {
            Drawable drawable = word_voice.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable) {
                ((AnimationDrawable) drawable).selectDrawable(0);
                ((AnimationDrawable) drawable).stop();
            }
        }

        @Override
        public void onError(int i, Object o) {
            Drawable drawable = word_voice.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable) {
                ((AnimationDrawable) drawable).selectDrawable(0);
                ((AnimationDrawable) drawable).stop();
            }

           // CWToast.centerError(mContex,"播放音频失败!").show();
            ToastUtil.INSTANCE.toastInCenter(mContex,"播放音频失败!");
        }

        @Override
        public void onBuffer(Object o, long l, long l1) {

        }

        @Override
        public void onPlaying(long l, long l1) {

        }
    };


    private void initView(){
        try{
            setOrientation(VERTICAL);
            item_1=findViewById(R.id.item_1);
            item_2=findViewById(R.id.item_2);
            item_3=findViewById(R.id.item_3);
            item_4=findViewById(R.id.item_4);
            item_1_result=findViewById(R.id.item_1_result);
            item_2_result=findViewById(R.id.item_2_result);
            item_3_result=findViewById(R.id.item_3_result);
            item_4_result=findViewById(R.id.item_4_result);

            word_voice=findViewById(R.id.word_voice);


            item_1.setOnClickListener(this);
            item_2.setOnClickListener(this);
            item_3.setOnClickListener(this);
            item_4.setOnClickListener(this);
            word_voice.setOnClickListener(this);
            AudioPlayer.getInstance().setOnPlayListener(playListener);
        }catch (Exception e){
            e.getStackTrace();
            nextStepLinstener.nextStep("VOICE_TO_CHINESE");
        }



    }


    public void SteDataAndUpdataUi(WordDetail currentWord,ArrayList<WordDetail> wordDetails, NextStepLinstener nextStepLinstener){

        try{
            if(currentWord==null||wordDetails.size()<4){
                return;
            }
            isLock=true;
            this.nextStepLinstener=nextStepLinstener;
            this.currentWord=currentWord;
            this.wordDetails=wordDetails;
            item_1.setText(wordDetails.get(0).getPretations());
            item_2.setText(wordDetails.get(1).getPretations());
            item_3.setText(wordDetails.get(2).getPretations());
            item_4.setText(wordDetails.get(3).getPretations());
            try {
                if(!NetworkUtils.isOnline()){
                    ToastUtil.INSTANCE.toastCenterNoNetError();
                }else {
                    AudioPlayer.getInstance().play(currentWord.getWordFile());
                    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M && EApplication.getInstance().mPlayerspeed !=1.0f) {
                        MediaPlayer mMediaPlayer = AudioPlayer.getInstance().getMediaPlayer();
                        if (mMediaPlayer != null) {
                            try {
                                PlaybackParams params = mMediaPlayer.getPlaybackParams().setSpeed(EApplication.getInstance().mPlayerspeed);
                                if (params != null) {
                                    mMediaPlayer.setPlaybackParams(params);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }

                }
            }catch (Exception e){
                e.getStackTrace();
            }
            item_1_result.setVisibility(GONE);
            item_2_result.setVisibility(GONE);
            item_3_result.setVisibility(GONE);
            item_4_result.setVisibility(GONE);

            item_1.setBackground(getResources().getDrawable(R.drawable.item_default_shape));
            item_2.setBackground(getResources().getDrawable(R.drawable.item_default_shape));
            item_3.setBackground(getResources().getDrawable(R.drawable.item_default_shape));
            item_4.setBackground(getResources().getDrawable(R.drawable.item_default_shape));

        }catch (Exception e){
            e.getStackTrace();
            nextStepLinstener.nextStep("VOICE_TO_CHINESE");
        }

    }
    private Handler mHandler = new Handler();
    @Override
    public void onClick(View view) {

        try {
            if (view.getId() == R.id.item_1) {
                if(isLock){
                    if (currentWord.getdId().equals(wordDetails.get(0).getdId())) {
                        isLock=false;
                        item_1_result.setVisibility(VISIBLE);
                        item_1_result.setImageResource(R.mipmap.right_img2);
                        item_1.setBackground(getResources().getDrawable(R.drawable.item_right_shape));
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_RIGHT, mContex, null);
                        //跳转下一题
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                nextStepLinstener.nextStep("VOICE_TO_CHINESE");

                            }
                        }, DELAY_TIME);
                    } else {
                        isLock=false;
                        item_1_result.setVisibility(VISIBLE);
                        item_1_result.setImageResource(R.mipmap.error_img);
                        item_1.setBackground(getResources().getDrawable(R.drawable.item_error_shape));
                        //跳转详情页
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_ERROR, mContex, null);
                        //跳转详情
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                WordJumpManager.jumpToReciteWordDetailActivity(R.string.go_back, (Activity) mContex, INTENT_FROM_WORD_STUDY_ACTIVITY, null, currentWord.getdId());


                            }
                        }, DELAY_TIME);

                    }
                }

            } else if (view.getId() == R.id.item_2) {
                if(isLock){
                    if (currentWord.getdId().equals(wordDetails.get(1).getdId())) {
                        isLock=false;
                        item_2_result.setVisibility(VISIBLE);
                        item_2_result.setImageResource(R.mipmap.right_img2);
                        item_2.setBackground(getResources().getDrawable(R.drawable.item_right_shape));
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_RIGHT, mContex, null);
                        //跳转下一题
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                nextStepLinstener.nextStep("VOICE_TO_CHINESE");

                            }
                        }, DELAY_TIME);

                    } else {
                        isLock=false;
                        item_2_result.setVisibility(VISIBLE);
                        item_2_result.setImageResource(R.mipmap.error_img);
                        item_2.setBackground(getResources().getDrawable(R.drawable.item_error_shape));
                        //跳转详情页
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_ERROR, mContex, null);
                        //跳转详情
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                WordJumpManager.jumpToReciteWordDetailActivity(R.string.go_back, (Activity) mContex, INTENT_FROM_WORD_STUDY_ACTIVITY, null, currentWord.getdId());

                            }
                        }, DELAY_TIME);

                    }
                }


            } else if (view.getId() == R.id.item_3) {
                if(isLock){
                    if (currentWord.getdId().equals(wordDetails.get(2).getdId())) {
                        isLock=false;
                        item_3_result.setVisibility(VISIBLE);
                        item_3_result.setImageResource(R.mipmap.right_img2);
                        item_3.setBackground(getResources().getDrawable(R.drawable.item_right_shape));
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_RIGHT, mContex, null);
                        //跳转下一题
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                nextStepLinstener.nextStep("VOICE_TO_CHINESE");

                            }
                        }, DELAY_TIME);

                    } else {
                        isLock=false;
                        item_3_result.setVisibility(VISIBLE);
                        item_3_result.setImageResource(R.mipmap.error_img);
                        item_3.setBackground(getResources().getDrawable(R.drawable.item_error_shape));
                        //跳转详情页
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_ERROR, mContex, null);
                        //跳转详情
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                WordJumpManager.jumpToReciteWordDetailActivity(R.string.go_back, (Activity) mContex, INTENT_FROM_WORD_STUDY_ACTIVITY, null, currentWord.getdId());

                            }
                        }, DELAY_TIME);

                    }
                }



            } else if (view.getId() == R.id.item_4) {
                if(isLock){
                    if (currentWord.getdId().equals(wordDetails.get(3).getdId())) {
                        isLock=false;
                        item_4_result.setVisibility(VISIBLE);
                        item_4_result.setImageResource(R.mipmap.right_img2);
                        item_4.setBackground(getResources().getDrawable(R.drawable.item_right_shape));
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_RIGHT, mContex, null);
                        //跳转下一题
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                nextStepLinstener.nextStep("VOICE_TO_CHINESE");

                            }
                        }, DELAY_TIME);

                    } else {
                        isLock=false;
                        item_4_result.setVisibility(VISIBLE);
                        item_4_result.setImageResource(R.mipmap.error_img);
                        item_4.setBackground(getResources().getDrawable(R.drawable.item_error_shape));
                        //跳转详情页
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_ERROR, mContex, null);
                        //跳转详情
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                WordJumpManager.jumpToReciteWordDetailActivity(R.string.go_back, (Activity) mContex, INTENT_FROM_WORD_STUDY_ACTIVITY, null, currentWord.getdId());

                            }
                        }, DELAY_TIME);

                    }
                }


            } else if (view.getId() == R.id.word_voice) {
                try {
                    if(!NetworkUtils.isOnline()){
                        ToastUtil.INSTANCE.toastCenterNoNetError();
                    }else {
                        AudioPlayer.getInstance().play(currentWord.getWordFile());
                        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M && EApplication.getInstance().mPlayerspeed !=1.0f) {
                            MediaPlayer mMediaPlayer = AudioPlayer.getInstance().getMediaPlayer();
                            if (mMediaPlayer != null) {
                                try {
                                    PlaybackParams params = mMediaPlayer.getPlaybackParams().setSpeed(EApplication.getInstance().mPlayerspeed);
                                    if (params != null) {
                                        mMediaPlayer.setPlaybackParams(params);
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                    }
                }catch (Exception e){
                    e.getStackTrace();
                }


            }

        } catch (Exception e) {
            e.getStackTrace();
            nextStepLinstener.nextStep("VOICE_TO_CHINESE");
        }
    }

}
